<?php
namespace app\common\model;
use app\common\cache\CacheData;

class MallGoods extends Base
{
    static $status = [
                0 => '下架',
                1 => '上架',
            ];

    protected $resultSetType = 'collection';

    public function cacheData(){
        return $this->select();
    }

    public static function status($key=null){
        if(!is_null($key)){
            return self::$status[$key];
        }else{
            return self::$status;
        }
    }

    public function getPageDatas($where=[],$page,$perpagenum,$order='id desc'){
        // 每页数量
        $start = ($page-1)*$perpagenum;
        $whereData=[];
        if(!empty($where)){
            foreach($where as $k => $wh):
                $whereData[$k] = $wh;
            endforeach;
        }
        $rows = $this
              ->where($whereData)
              ->limit($start,$perpagenum)
              ->order($order)
              ->select();
        $count = $this->alias('a')->where($whereData)->count();
        $data = [];
        if($count){
            foreach($rows as $row):
                $row = $row->toArray();
                $data[] = $this->setField($row);
            endforeach;
        }

        $total_page = ceil($count/$perpagenum);
        return [
            'total_page' => $total_page,
            'curent_page' => $page,
            'count' => $count,
            'data' => $data
        ];
    }

    public function getOneDataById($id,$fields=[]){
        $row = $this->field($fields)->find($id);
        if(!$row){
            $this->error='商品不存在';
            return false;
        }
        $row = $row->toArray();
        $row = $this->setField($row);
        return $row;
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setField($row){
        if(isset($row['coin_id'])){
            $coin_name = Coin::coinData($row['coin_id']);
            $row['coin_name'] = $coin_name['coin_name'];
        }
        if(isset($row['cate_id'])){
            $cate_name = model('MallCategory')->getCategoryDetail($row['cate_id']);
            $row['cate_name'] = $cate_name['title'];
        }
        if(isset($row['create_time'])){
            $row['create_time'] = date("Y-m-d H:i",$row['create_time']);
        }
        if(isset($row['status'])){
            switch ($row['status']) {
                case '1':
                    $row['is_status'] = '上架';
                    break;
                default:
                    $row['is_status'] = '下架';
                    break;
            }
        }
        if(isset($row['is_tj'])){
            switch ($row['is_tj']) {
                case '1':
                    $row['is_tjs'] = '推荐';
                    break;
                default:
                    $row['is_tjs'] = '普通';
                    break;
            }
        }
        if(isset($row['city'])){
            // 地址处理
            $code = $row['city'];
            // 省
            $row['address_province'] = substr($row['city'], 0,strlen($row['city'])-4);
            // 市
            $row['address_city'] = substr($row['city'], 0,strlen($row['city'])-2);
            // 县
            $row['address_county'] = $row['city'];
        }
        if(isset($row['content'])){
            $row['content'] = strip_tags(htmlspecialchars_decode($row['content']));
        }
        return $row;
    }

    /**
     * 新增商品
     * @param [type] $data [description]
     */
    public function addOneData($data=[]){
        return $this->save($data);
    }
    /**
     * [deleteOneData 删除商品]
     * @param  string $data [description]
     * @return [type]       [description]
     */
    public function deleteOneData($data=''){
        return $this->where(['id'=>$data])->delete();
    }

    /**
     * 更新会员资料
     * @return [type] [description]
     */
    public function saveOneData($data=[],$id=''){
        return $this->update($data,['id' => $id]);
    }

    /**
     * 判断商品库存
     * @return [type] [description]
     */
    public function goodsTotal($data=[]){
        $res = $this->where($data)->field('id')->find();
        if($res){
           return false;
        }
        return true;
    }

    public function decTotal($where=[],$row=0){
        return $this->where($where)->setDec('total',$row);
    }

    /**
     * [decTotal 商品数量自增]
     * @param  array   $where [description]
     * @param  integer $row   [description]
     * @return [type]         [description]
     */
    public function incTotal($where=[],$row=0){
        return $this->where($where)->setInc('total',$row);
    }

    /**
     * [decTotal 自增]
     * @param  array   $where [description]
     * @param  integer $row   [description]
     * @return [type]         [description]
     */
    public function incNum($where=[],$field='',$row=0){
        return $this->where($where)->setInc($field,$row);
    }

    public function getValue($where=[],$field=''){
        return $this->where($where)->value($field);
    }
}